﻿using LayuiMvc.DataEntity;
using LayuiMvc.DataModel.Community;
using LayuiMvc.Logic.LogicEnum;
using LayuiMvcUtil.Result;
using System.Collections.Generic;
using System.Threading.Tasks;
using System.Data.Entity;
using System.Linq;
using LayuiMvc.DataEntity.DataEnum;

namespace LayuiMvc.Logic.Community
{
    public class PostManageService
    {

        public async Task<DataResult<List<PostManageModel>>> PostManageListAsync(int pageIndex,int pageSize,
            PostTypeQueryEnum postType,
            PostStatusQueryEnum[] postStatusArray,
            PostSortQueryEnum postSort)
        {
            DataResult<List<PostManageModel>> rtnResult = new DataResult<List<PostManageModel>>();

            using(EFContext db=new EFContext())
            {
                var query = db.Posts.Include(m => m.Replys)
                    .Include(m => m.PublishUser)
                    .Where(m => !m.IsDelete);

                //帖子分类
                if (postType!=PostTypeQueryEnum.None)
                {
                    PostTypeEnum postTyepeEnum = (PostTypeEnum)postType;
                    query = query.Where(m => m.PostType == postTyepeEnum);
                }

                //帖子状态

                if (postStatusArray!=null)
                {
                    foreach (var postStatus in postStatusArray)
                    {
                        switch (postStatus)
                        {
                            case PostStatusQueryEnum.Top:
                                query = query.Where(m => m.IsTop);
                                break;
                            case PostStatusQueryEnum.UnEnd:
                                query = query.Where(m => !m.IsEnd);
                                break;
                            case PostStatusQueryEnum.End:
                                query = query.Where(m => m.IsEnd);
                                break;
                            case PostStatusQueryEnum.Cream:
                                query = query.Where(m => m.IsCream);
                                break;
                        }
                    }
                }

                //帖子排序
                switch (postSort)
                {
                    case PostSortQueryEnum.New:
                        query = query.OrderByDescending(m => m.PublishTime);
                        break;
                    case PostSortQueryEnum.Hot:
                        query = query.OrderByDescending(m => m.Replys.Count);
                        break;
                    default:
                        break;
                }

                rtnResult.count = await query.CountAsync();

                List<PostManageModel> postList = await query.Skip((pageIndex - 1) * pageSize).Take(pageSize)
                    .Select(m => new PostManageModel()
                    {
                        PostId = m.PostId,
                        Title = m.Title,
                        PublishTime = m.PublishTime,
                        ReplyCount = m.Replys.Count,
                        ViewCount = m.ViewCount,
                        PostType = m.PostType,
                        IsTop = m.IsTop,
                        IsCream = m.IsCream,
                        IsEnd = m.IsEnd,
                        PublishUserName = m.PublishUser.NickName
                    }).ToListAsync();

                rtnResult.data = postList;
                rtnResult.page = pageIndex;
                rtnResult.limit = pageSize;

                return rtnResult;
            }
        }
    }
}
